package c.x.jy.admin.controller;

import java.util.Arrays;
import java.util.Map;

import c.x.jy.dao.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import c.x.jy.dao.entity.SysRoleEntity;
import c.x.jy.service.service.SysRoleService;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.R;



/**
 * 角色
 *
 * @author amwhyh
 * @email xxx@xx.com
 * @date 2021-05-20 20:25:55
 */
@RestController
@RequestMapping("sys/sysrole")
public class SysRoleController extends AbstractController {
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysrole:list")
    public R list(@RequestParam Map<String, Object> params){
     	if(getUser().getBusinessId()>0) {
    		params.put("businessId", getUser().getBusinessId());
    	}
        PageUtils page = sysRoleService.queryPage(params);

        return R.put(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{roleId}")
    @RequiresPermissions("sys:sysrole:info")
    public R info(@PathVariable("roleId") Long roleId){
		SysRoleEntity sysRole = sysRoleService.getById(roleId);

        return R.put(sysRole);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysrole:save")
    public R save(@RequestBody SysRoleEntity sysRole){
		sysRoleService.save(sysRole);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysrole:update")
    public R update(@RequestBody SysRoleEntity sysRole){
		sysRoleService.updateById(sysRole);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{roleId}")
    @RequiresPermissions("sys:sysrole:delete")
    public R delete(@PathVariable("roleId") Long roleId){
		sysRoleService.removeByIds(Arrays.asList(roleId));

        return R.ok();
    }


    @GetMapping("/allRoles")
    @RequiresPermissions("sys:sysrole:list")
    public R allRoles(Long userId){

        SysUserEntity user = new SysUserEntity();
     if(userId!=null)   user.setUserId(userId);
        return R.put(sysRoleService.selectRolesByUserId( user));
    }


}

